package com.techshine.util;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheException;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

public class EhcacheUtil

{



    private static CacheManager cacheManager  = CacheManager.create();

    private static Cache ScanCache = null;





        public static void getCache(String url){


            try {



                //System.out.println("url"+url);

                url=url+"\\ehcache.xml";






                cacheManager =  CacheManager.create(url);




                ScanCache = cacheManager.getCache("ScanCache");
            } catch (CacheException e) {
                e.printStackTrace();
            }




        }


    public static CacheManager getCacheManager() {




        return cacheManager;
    }

    public  static Cache getScanCache( ) {

            if(ScanCache==null){

           //     getCache(url);

                ScanCache = cacheManager.getCache("ScanCache");

            }else{



            }

        return ScanCache;
    }



    public static void main(String[] args) {
        // 1. 创建缓存管理器
      CacheManager cacheManager = CacheManager.create("./src/main/resources/ehcache.xml");
        //       CacheManager cacheManager = CacheManager.create("src/main/resources/ehcache.xml");
        // 2. 获取缓存对象
        Cache cache = cacheManager.getCache("ScanCache");

        // 3. 创建元素
        Element element = new Element("1213", "1231");

        // 4. 将元素添加到缓存
        cache.put(element);

        // 5. 获取缓存
        Element value = cache.get("1213");
        System.out.println("value: " + value);
        System.out.println(value.getObjectValue());


        System.out.println(cache.getSize());

        // 7. 刷新缓存
        cache.flush();

        // 8. 关闭缓存管理器
        cacheManager.shutdown();

    }






}
